VIA-942: Enforce Session Expiry Reliably After 10 Minutes of Inactivity#708
VIA-942: Enforce Session Expiry Reliably After 10 Minutes of Inactivity#708liming-cheung-nhs wants to merge 11 commits intomainfrom
Conversation
… recently called Precommit hooks bypassed; this is a work in progress commit WIP commit: contains * A Server Action to set the flag cookie * Calling the server action from the user-logout method * cherck the flag in getToken Issue under investigation 1. getToken will return null, but the MyVaccines app then currently tries to redirect the user to NHS App because they are not authenticated, meaning the user will never see the logout / session timeout screens. (at present this throws a cors error in the browser and the user never actually gets to NHS app but this is a quirk and the underlying issue still needs fixing; the user shouldnt be redirected at all for this part of the journey. The network tab shows the cors error and an error would be logged in our service logs).
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
There was a problem hiding this comment.
Pull request overview
Implements a short-lived “signout flag” cookie to prevent session refresh logic from re-authenticating a user immediately after logout/inactivity timeout, improving enforcement of 10-minute inactivity expiry.
Changes:
- Add
__Secure-signoutcookie constant and document it in the cookies policy table. - Set the signout-flag cookie during logout and check it in the NextAuth
jwt/getTokencallback to returnnullfor recently-signed-out sessions. - Add/extend unit tests for the new logout flow, cookie setting, and token callback behavior.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| src/utils/constants.ts | Adds constant for the new signout-flag cookie name. |
| src/utils/auth/user-logout.ts | Converts logout to a server action, wraps in request-scoped storage, and sets signout-flag cookie before signing out. |
| src/utils/auth/user-logout.test.ts | Updates mocks and adds assertions for flag cookie + request-scoped wrapper usage. |
| src/utils/auth/setSignOutFlagCookie.ts | New server action to set a short-lived signout-flag cookie tied to the session id. |
| src/utils/auth/setSignOutFlagCookie.test.ts | New unit test validating the signout cookie is set as expected. |
| src/utils/auth/callbacks/get-token.ts | Reads signout-flag + session-id cookies to short-circuit and return null after recent signout. |
| src/utils/auth/callbacks/get-token.test.ts | Adds tests ensuring getToken returns null when signout flag matches current session. |
| src/app/our-policies/cookies-policy/CookiesTable.tsx | Documents the new __Secure-signout cookie and its purpose/expiry. |
| src/app/our-policies/cookies-policy/CookiesTable.test.tsx | Updates expectations to include the new cookie row and timing. |
| src/app/_components/inactivity/InactivityDialog.test.tsx | Adjusts mocking for updated logout module usage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <copilot@github.com>
|
|
||
| const log = logger.child({ module: "utils-auth-setSignOutFlagCookie" }); | ||
|
|
||
| const setSignOutFlagCookie = async () => { |
There was a problem hiding this comment.
I had to split the logout flow back again because the two steps need different runtimes.
The signout cookie must be set on the server so it can use request cookies and requestScopedStorageWrapper.
The actual signOut() stays on the client because when both steps were moved to the server, the logout response no longer preserved the signout cookie reliably, so the logout flow stopped working properly. Keeping signOut() on the client preserves the cookie step and still gives the expected immediate logout and redirect.
Co-authored-by: Copilot <copilot@github.com>
413fe4b to
ddf571b
Compare
| maxAgeInSeconds, | ||
| ); | ||
| }); | ||
|
|
There was a problem hiding this comment.
Is this not a duplicate tests as what is added above?
| }; | ||
|
|
||
| it("should set signout cookie with the current session id", async () => { | ||
| mockGetCookie = (name: string) => { |
There was a problem hiding this comment.
Is this duplicated mocking needed as its the same as above?
Description
Testing on deployed to sandpit personal workspace
30/04/2026
I was getting this Client side error- UNHANDLED_PROMISE_REJECT_ERROR on logout when put
signoutand cookie set in server.signoutneeds to be in client given current UI implementation - see comment in code. The error went away when movedsignoutfrom server.Note: Existing issue under investigation:
getTokenwill return null, but the MyVaccines app then currently tries to redirect the user to NHS App because they are not authenticated, meaning the user will never see the logout / session timeout screens. (at present this throws a cors error in the browser and the user never actually gets to NHS app but this is a quirk and the underlying issue still needs fixing; the user shouldnt be redirected at all for this part of the journey. The network tab shows the cors error and an error would be logged in our service logs).Context
Type of changes
Checklist
Sensitive Information Declaration
To ensure the utmost confidentiality and protect your and others privacy, we kindly ask you to NOT including PII (Personal Identifiable Information) / PID (Personal Identifiable Data) or any other sensitive data in this PR (Pull Request) and the codebase changes. We will remove any PR that do contain any sensitive information. We really appreciate your cooperation in this matter.